package com.peaker.advertisingpro.view

import android.app.Dialog
import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.Gravity
import android.view.LayoutInflater
import android.view.Window
import android.view.WindowManager
import android.webkit.WebView
import android.webkit.WebViewClient
import com.peaker.advertisingpro.R
import com.peaker.advertisingpro.bean.LinkUrl
import kotlinx.android.synthetic.main.layout_dialogfull.*

/**
 * webviewdialog
 * @constructor
 */
class FullScreenDialog(context: Context, var lin: LinkUrl) : Dialog(context) {

    init {
        initView()
    }

    private fun initView() {
        window?.requestFeature(Window.FEATURE_NO_TITLE)

        window?.setBackgroundDrawable(ColorDrawable(0x00000000))
        var leve = when (lin.level!!) {
            "left" -> Gravity.LEFT
            "center" -> Gravity.CENTER_HORIZONTAL
            "right" -> Gravity.RIGHT
            else -> Gravity.CENTER_HORIZONTAL
        }
        var vertical = when (lin.vertical!!) {
            "top" -> Gravity.TOP
            "center" -> Gravity.CENTER_VERTICAL
            "bottom" -> Gravity.BOTTOM
            else -> Gravity.CENTER_VERTICAL
        }
        window.setGravity(leve or vertical)

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        var inflate = LayoutInflater.from(context).inflate(R.layout.layout_dialogfull, null)
        setContentView(inflate)
        webview.webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
                view.loadUrl(url)
                return true
            }
        }
        webview.loadUrl(lin.url)

    }

    override fun dismiss() {

        super.dismiss()

    }
    override fun show() {
        super.show()
        window?.setLayout(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT
        )
        var attributes = window?.attributes
        val windowManager =
            getContext().getSystemService(Context.WINDOW_SERVICE) as WindowManager
        val displayMetrics = DisplayMetrics()
        windowManager.defaultDisplay.getMetrics(displayMetrics)
        attributes?.width =(displayMetrics.widthPixels *( lin.size!!/100)).toInt()
        attributes?.height =(displayMetrics.heightPixels * (lin.size!!/100)).toInt()
        window?.attributes = attributes
    }
}